package com.xl.rentkeeper.act;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.CheckBox;
import android.widget.ListView;

import com.xl.rentkeeper.R;
import com.xl.rentkeeper.adapter.PushAdapter;
import com.xl.rentkeeper.business.MsgLogic;
import com.xl.rentkeeper.mgr.IUiObserver;
import com.xl.rentkeeper.mgr.UiObserverManager;
import com.xl.rentkeeper.msg.Message;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/7/21.
 */
public class Push extends BaseAct implements IUiObserver {
    public ListView mLVPush;
    public List<Message> mPullList = new ArrayList<>();
    public PushAdapter mAdapter = null;
    public CheckBox mCKToBom;
    private SwipeRefreshLayout mSRLPush;
    public String[] monitorEvents() {
        return new String[]{CMD_MSG_PULL};
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_push);
        initView();
    }

    public void initView() {
      //  View view = View.inflate(this, R.layout.fragment_push,null);
        mLVPush = (ListView) findViewById(R.id.lv_push);
        mSRLPush = (SwipeRefreshLayout) findViewById(R.id.srl_push);
        mSRLPush.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
        mSRLPush.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                MsgLogic.getInstance().pullMsg();
            }
        });
        mCKToBom = (CheckBox)  findViewById(R.id.ck_tobom);
        mAdapter = new PushAdapter(this,mPullList);
        mLVPush.setAdapter(mAdapter);

        List<Message> list = MsgLogic.getInstance().getMessages();
        if(list!=null && list.size()!=0){
            mPullList.clear();
            mPullList.addAll(list);
            mAdapter.notifyDataSetChanged();
        }
        UiObserverManager.getInstance().registerEvent(monitorEvents(), this);
        //return view;
    }
    @Override
    public void onEvent(String event, boolean ret,String errInfo, Object[] data) {
        if(event.equals(CMD_MSG_PULL)){
            mSRLPush.setRefreshing(false);
            List<Message> list = MsgLogic.getInstance().getMessages();
            if(list!=null && list.size()!=0){
                mPullList.clear();
                mPullList.addAll(list);
                mAdapter.notifyDataSetChanged();
            }
        }
    }
}
